package designsBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;
import tools.ReadProperties;
import tools.UUid;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/12/19.
 * 设计稿页面处理方法
 */
public class DesignsDispense implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        JSONObject jsonObject = GetRequestInputStream.getJson(request);
        String action = jsonObject.getString("action");
        String splitMark = myConfig.getSplitMark("splitMark");
        String appPath = myConfig.getPropertiesConfig().getProperties().getProperty("appPath");
        String designsPath = splitMark + "data" + splitMark + "designs" + splitMark + "koufu" + splitMark;
        ReadProperties rp = new ReadProperties(appPath);
        String designDirStr = rp.getProperties().getProperty("designDir");
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);

        //给出列表
        if (Objects.equals(action, "getList")){
            String createtime = jsonObject.getString("createtime");
            String where = "";
            if (!createtime.equals("")){
                where = "WHERE createtime LIKE \"" + createtime + "%\"";
            }
            String sql = "SELECT `name`, `dirname`, `detail`, `createtime` FROM `design` "+where+" ORDER BY `createtime` DESC";
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            JSONArray dirList = new JSONArray();
            for (ArrayList<String> arrayList: sqlReturnModel.getSelectReturnData()){
                JSONArray one = new JSONArray();
                String name = arrayList.get(0);
                String dirname = arrayList.get(1);
                String detail = arrayList.get(2);
                String cretime = arrayList.get(3);

                dirname = Objects.equals(dirname,null)||dirname.equals("") ? name : dirname;
                one.add(name);
                one.add(dirname);
                dirList.add(one);
            }
            returnJson.setApiStatus(1);
            returnJson.setResult(dirList);
            return returnJson.toString();
        }

        //给出详细目录
        if (Objects.equals(action, "getDetailPath")){
            String name = jsonObject.getString("name");
            File f2 = new File(appPath + designsPath + name + splitMark + "a" + splitMark);
            String middleName = f2.listFiles()[0].getName();
            String path = designsPath + name + splitMark + "a" + splitMark + middleName + splitMark + "start.html";
            returnJson.setApiStatus(1);
            returnJson.setResult(path);
            return returnJson.toString();
        }

        //解压压缩包
        if (Objects.equals(action, "unzip")){
            String uuid = UUid.getUUid();
            String f = jsonObject.getString("fileName");
            String fileName = f.substring(f.lastIndexOf("\\") + 1, f.length());
            String version = jsonObject.getString("version").trim();    //这个页面上对应的是版本号，数据库对应的是name
            String aPath = appPath + designsPath + uuid + splitMark + "a";

            //创建a目录
            File file = new File(appPath + designsPath + uuid);
            File aDir = new File(aPath);
            file.mkdir();
            aDir.mkdir();
            //"C:\Program Files\WinRAR\WinRAR.exe" e -or 口福V2.4确认版-20191218-YXY.zip 2.4
            String whereWinrar = myConfig.getPropertiesConfig().getProperties().getProperty("winrarPath");
            String system = myConfig.getPropertiesConfig().getProperties().getProperty("system");

            String command = "";
            if (Objects.equals(system, "win")){
                //win下用winrar解压
                command = whereWinrar + " x -y " + appPath + designsPath + fileName + " " + aPath;
            }
            if (Objects.equals(system, "linux")){
                //linux下用unzip解压 unzip -o xxx.zip -d /解压目录
                command = whereWinrar + " -o " + appPath + designsPath + fileName + " -d " + aPath;
            }

            System.out.println(command);
            Runtime runtime = Runtime.getRuntime();
            try {
                Process process = runtime.exec(command);
                //下面的 for 是验证目录里有没有东西
                for (int i = 0; i < 10; i++){
                    File f3 = new File(aPath);
                    if (!Objects.equals(f3.listFiles(), null) && (f3.listFiles().length != 0)) {
                        break;
                    }
                    Thread.sleep(1000);
                }
                //把数据录库
                String sql = "INSERT design SET `name`=\""+version+"\", dirname=\""+uuid+"\", type=\"1\"";
                sqlDispatch.insert(sql);

            }catch (Exception e){
                e.printStackTrace();
            }

            returnJson.setApiStatus(1);
            returnJson.setResult("ok");
            return returnJson.toString();
        }

        returnJson.setResult(0);
        returnJson.setErrorMsg("action not support");
        return null;
    }

    private boolean delFile(File file) {
        if (!file.exists()) {
            return false;
        }

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                delFile(f);
            }
        }
        return file.delete();
    }
}
